package org.example.springbootrabbitmq.normal;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class TtlRabbitmqConfig {

    public static final String X_EXCHANGE = "X";
    public static final String Y_DEAD_EXCHANGE = "Y";

    public static final String X_QUEUE_A = "QA";
    public static final String X_ROUTING_KEY_A = "XA";
    public static final String X_QUEUE_B = "QB";
    public static final String X_QUEUE_C = "QC";
    public static final String X_ROUTING_KEY_B = "XB";
    public static final String Y_DEAD__QUEUE = "QD";
    public static final String Y_DEAD_ROUTING_KEY = "YD";
    private static final String X_ROUTING_KEY_C = "XC";


    @Bean("x_exchange")
    public DirectExchange xExchange() {
        return ExchangeBuilder.directExchange(X_EXCHANGE).durable(true).build();
    }
    @Bean("y_dead_exchange")
    public DirectExchange yDeadExchange() {
        return ExchangeBuilder.directExchange(Y_DEAD_EXCHANGE).durable(true).build();
    }
    // 消息过期后，自动转发到死信队列
    @Bean("queueA")
    public Queue xQueueA() {
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", Y_DEAD_EXCHANGE);
        arguments.put("x-dead-letter-routing-key", "YD");
        arguments.put("x-message-ttl", 10000);
        return QueueBuilder.durable(X_QUEUE_A).withArguments(arguments).build();
    }

    // 消息过期后，自动转发到死信队列
    @Bean("queueB")
    public Queue xQueueB() {
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", Y_DEAD_EXCHANGE);
        arguments.put("x-dead-letter-routing-key", "YD");
        arguments.put("x-message-ttl", 40000);
        return QueueBuilder.durable(X_QUEUE_B).withArguments(arguments).build();
    }

    // 死信队列
    @Bean("queueD")
    public Queue yDeadQueue() {
        return QueueBuilder.durable(Y_DEAD__QUEUE).build();
    }

    // 绑定交换机和队列
    @Bean()
    public Binding xBinding1(@Qualifier("queueA") Queue xQueueA, @Qualifier("x_exchange") DirectExchange xExchange) {
        return BindingBuilder.bind(xQueueA).to(xExchange).with(X_ROUTING_KEY_A);
    }

    @Bean()
    public Binding xBinding2(@Qualifier("queueB") Queue xQueueB, @Qualifier("x_exchange") DirectExchange xExchange) {
        return BindingBuilder.bind(xQueueB).to(xExchange).with(X_ROUTING_KEY_B);
    }

    @Bean()
    public Binding YBinding(@Qualifier("queueD") Queue xQueueD, @Qualifier("y_dead_exchange") DirectExchange yDeadExchange) {
        return BindingBuilder.bind(xQueueD).to(yDeadExchange).with(Y_DEAD_ROUTING_KEY);
    }

    @Bean("queueC")
    public Queue xQueueC() {
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", Y_DEAD_EXCHANGE);
        arguments.put("x-dead-letter-routing-key", "YD");
        return QueueBuilder.durable(X_QUEUE_C).withArguments(arguments).build();
    }
    @Bean()
    public Binding xBinding3(@Qualifier("queueC") Queue xQueueC, @Qualifier("x_exchange") DirectExchange xExchange) {
        return BindingBuilder.bind(xQueueC).to(xExchange).with(X_ROUTING_KEY_C);
    }
}
